# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.jsonUtils_v2.table2Json_YM import Table2JsonYM
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField_YM
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
import time
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

#localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
class UrlManageDao(object):
    def __init__(self):  # 类初始化。
        self.conn = MysqlUtils.get_mysql_connections_v2()  # 根据类名调用get_mysql_connections()方法，连接到数据库



    def selectUrlManage(self, id):
        """
        根据ID查询信息，返回元组

        """
        sql = "select * from zzh_url_manage WHERE id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [id])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr



    def add_url_manage(self, col_info):

        level_code = col_info['level_code']
        dept_name = col_info['dept_name']
        dept_url = col_info['dept_url']
        des_url = col_info['des_url']
        evidence_url = col_info['evidence_url']
        face_keywords = col_info['face_keywords']
        opposite_keywords = col_info['opposite_keywords']
        check_status = col_info['check_status']
        balance_status = col_info['balance_status']

        check_time = col_info['check_time']

        province_id = col_info['province_id']
        city_id = col_info['city_id']
        district_id = col_info['district_id']

        enter_person = col_info['enter_person']
        enter_time = col_info['enter_time']
        check_remark = col_info['check_remark']
        check_person = col_info['check_person']
        dept_status = col_info['dept_status']

        sql = "insert into zzh_url_manage (level_code,dept_name,dept_url,des_url,evidence_url,face_keywords," \
              "opposite_keywords,check_status,balance_status,check_time,province_id," \
              "city_id,district_id,enter_person,enter_time,check_remark,check_person,dept_status) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [level_code, dept_name, dept_url, des_url, evidence_url, face_keywords,
                               opposite_keywords, check_status, balance_status, check_time,province_id,
                               city_id, district_id,enter_person,enter_time,check_remark,check_person,dept_status])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def del_url_manage(self, ids):
        cur = self.conn.cursor()
        sql = "delete from zzh_url_manage WHERE id=%s"
        for url_manage_id in ids:
            cur.execute(sql, [url_manage_id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return 1

    def query_url_manage(self, url_manage_id):

        sql = "select * from url_manage_view WHERE id=%s "
        cur = self.conn.cursor()
        totalCount = cur.execute(sql, [url_manage_id])
        if totalCount:
            url_manage_list = cur.fetchall()
            cur.close()
            self.conn.close()
            url_manage_json = Table2JsonYM.view_url_manage_listto_json(url_manage_list)
            return url_manage_json,totalCount

        else:
            totalCount = 0
            return None,totalCount

    def queryUrlManageByCondition(self,**kwargs):

        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']




        fieldMap = requestFieldMapToDBField_YM.viewUrlManageResuestFieldMap#要改的这个
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM url_manage_view WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM url_manage_view WHERE " + whereSentence
        rs = cur.execute(sql)

        total_count = cur.execute(sql)
        if total_count:
            result = cur.fetchall()

            cur.close()
            self.conn.close()
            resultList= MyPaging().getPage(total_count,pageIndex,pageSize,result)
            userOauthJSON = Table2JsonYM.view_url_manage_listto_json(resultList)
            return userOauthJSON,total_count
        else:

            cur.close()
            self.conn.close()
            return None,0




    def update_url_manage(self, new_url_manage, url_manage_id):
        level_code = new_url_manage['level_code']
        dept_name = new_url_manage['dept_name']
        dept_url = new_url_manage['dept_url']
        des_url = new_url_manage['des_url']
        evidence_url = new_url_manage['evidence_url']
        face_keywords = new_url_manage['face_keywords']
        opposite_keywords = new_url_manage['opposite_keywords']
        check_status = new_url_manage['check_status']
        balance_status = new_url_manage['balance_status']

        check_time = new_url_manage['check_time']

        province_id = new_url_manage['province_id']
        city_id = new_url_manage['city_id']
        district_id = new_url_manage['district_id']

        enter_person = new_url_manage['enter_person']
        enter_time = new_url_manage['enter_time']
        check_remark = new_url_manage['check_remark']
        check_person = new_url_manage['check_person']
        dept_status = new_url_manage['dept_status']
        cur = self.conn.cursor()
        sql = "UPDATE zzh_url_manage SET level_code=%s, dept_name=%s, dept_url=%s, des_url=%s, evidence_url=%s, face_keywords=%s, " \
              "opposite_keywords=%s, check_status=%s, balance_status=%s, check_time=%s,  province_id=%s, " \
              "city_id=%s, district_id=%s ,enter_person=%s, enter_time=%s, check_remark=%s, check_person=%s, dept_status=%s WHERE id=%s "
        update_url_manage = cur.execute(sql, [level_code, dept_name, dept_url, des_url, evidence_url, face_keywords,
                                             opposite_keywords, check_status, balance_status, check_time,
                                             province_id, city_id, district_id,enter_person,enter_time,check_remark,check_person,dept_status, url_manage_id])
        self.conn.commit()
        if update_url_manage:
            cur.close()
            self.conn.close()
            return 1
        else:
            cur.close()
            self.conn.close()
            return 0


    def updateUrlManageCheckStatus(self,checkStatus,ids,checkPerson,checkRemark): #checkRemark,审核意见
        cur = self.conn.cursor()
        if checkPerson == None:
            check_time = None
        else:
            check_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

        for (i, j) in zip(ids, checkStatus):#zip内部函数会将参数按照序列对应组成 [(ids[0],checkStatus[0]),(ids[1],checkStatus[1]),(ids[2],checkStatus[2]),……]这种
            sql = "UPDATE zzh_url_manage SET check_status=%s,check_time=%s,check_person=%s,check_remark=%s WHERE id=%s"
            rs=cur.execute(sql, [j, check_time, checkPerson, checkRemark, i])
            self.conn.commit()
        if rs:
            cur.close()
            self.conn.close()
            return 1
        else:
            cur.close()
            self.conn.close()
            return 0

